<?php
/**
 * Created by PhpStorm.
 * User: janke
 * Date: 17-3-24
 * Time: 上午8:54
 */

namespace app\models;
use App\Http\Controllers\Controller;
use yii\base\Model;
use yii\db\ActiveRecord;
use yii\filters\auth\CompositeAuth;

class Comment extends ActiveRecord
{

    public $commentSender;
    public $messageId;
    public $commentContent;
    public $accessTaken;

    /**
     * 数据表
     * @author janke
     * @return string
     */
    public static function tableName()
    {
        return 'comment'; // TODO: Chan ge the autogenerated stub
    }

    public function fields()
    {
        return [
            'commentId' => 'id',
            'commentSender' => 'username',
            'commentContent' => 'content',
            'commentTime' => 'time',
//            'commentIsFake' => 'isfake',
//            'commentFakeName' => 'fakename',
//            'commentReciverId' => 'father_id',
            'messageId' => 'message_id',
            'commentApproveCount' => 'approve_count',
            'commentDisapproveCount' => 'disapprove_count',
        ]; // TODO: Change the autogenerated stub
    }

    /**
     * 根据messageId找到该message的所有评论并且格式键名
     * @param $messageId
     * @return array
     * @author Janke
     */
    public static function findByMessageId($messageId)
    {
        $messageComments = Comment::findAll(['message_id' => $messageId]);
        $commentsArray = [];
        foreach ($messageComments as $comment)
        {
            $data = self::formatData($comment);
            array_push($commentsArray, $data);
        }
        return $commentsArray;
    }

    /**
     * 根据commentId找到评论对象
     * @param $commentId
     * @return null|static Comment
     * @author Janke
     */
    public static function findByCommentId($commentId)
    {
        $comment = Comment::findOne(['id' => $commentId]);
        if ($comment instanceof Model)
        {
//            print_r($comment->attributes);
            return $comment;
        }
        return null;
    }

    /**
     * 格式化键值对的键名
     * @param $comment
     * @return array
     * @author Janke
     */
    public static function formatData($comment)
    {
        $format = $comment->fields();
        $attributes = $comment->attributes;
        $data = [];
        foreach ($format as $key => $value)
        {
            $data[$key] = $attributes[$value];
        }
        return $data;
    }

    /**
     * 插入一条评论，更新message的评论数
     * @return bool|static
     * @author janke
     */
    public function insertComment()
    {
        $rest = \Yii::$app->db->createCommand()->insert('comment', [
            'message_id' => $this->messageId,
            'content' => $this->commentContent,
            'username' => $this->commentSender,

        ])->execute();
        $update = Message::updateCommentCount($this->messageId);
        return ($rest && $update) ? $rest : false;
    }

    public function deleteCommentById($commentId, $commentModel)
    {
        $comment = Comment::findByCommentId($commentId);
        $user = User::findUserDataByAccessToken(User::$accessToken);
        if (!$comment)
        {
            $commentModel->addError("error", "comment not found");
            return false;
        }
        $commentArray = Comment::formatData($comment);
        if ($user['userName'] != $commentArray['commentSender'])
        {
            $commentModel->addError("error", "you have not right to delete this comment");
            return false;
        }
        return $comment->delete();
    }

    public function rules()
    {
        return [
            [['messageId', 'commentContent', 'commentSender'], 'required'],
        ]; // TODO: Change the autogenerated stub
    }
}